package student_system.Dao;


import student_system.UnderAccessDBclass.jdbcUtils;
import student_system.bean.User;

import java.sql.ResultSet;
import java.sql.SQLException;



public class UserDaoImpl implements IUserDao{
    //初始化预编译对象，结果集，数据库连接

    ResultSet rs = null;


    @Override
    //登录
    public User find(User user) {
        //获取连接
        User us=new User();
        String sql = "select * from users where username=? and password=?";
        Object[] params=null;
        params[0]=(Object)user.getUsername();
        params[1]=(Object)user.getPassword();
        rs= jdbcUtils.executeQuery(sql, params);
        try {
            while(rs.next()) {
                try {
                    us.setUsername((String)rs.getObject(1));
                    us.setPassword((String)rs.getObject(2));
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return user;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    //注册
    public boolean registerUser(User user) {
        //获取连接
        Object[] params=null;
        params[0]=(Object)user.getUsername();
        params[1]=(Object)user.getPassword();
        String sql = "insert into users(username,password) values(?,?)";
        Boolean n=jdbcUtils.executeUpdate(sql, params);
        return n;
    }
}
